package com.example.demo.Controller;

import com.example.demo.Model.User;
import com.example.demo.Service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Resource
    private UserService userService;
//    @Autowired
//    SendMailService sendMailService;

    @RequestMapping("/StudentPage")
    public String StudengPage(Model model, HttpSession session) {
        if(session.getAttribute("username") !=null)
            return "student/StudentPage";
        else
            return "login";
    }
    @RequestMapping("/TeacherPage")
    public String TeacherPage(Model model, HttpSession session) {
        if(session.getAttribute("username") !=null)
            return "teacher/TeacherPage";
        else
            return "login";
    }

    @RequestMapping("/login")
    public String  login(HttpSession session){
            return  "login";
    }
    @RequestMapping("/register")
    public String  register(HttpSession session){
            return  "register";
    }

    @RequestMapping("/login_user")
    public String login_user(String username,String password,HttpSession session){
        User user=new User();
        user.setPassword(password);
        user.setUsername(username);
        if (userService.verifyLogin(user).equals("1")){
            session.setAttribute("stuID",username);
            session.setAttribute("stupassword",password);
            session.setAttribute("useright",1);
            return "student/StudentPage";
        }else if(userService.verifyLogin(user).equals("2")){
            session.setAttribute("teID",username);
            session.setAttribute("tepassword",password);
            session.setAttribute("useright",2);
            return "teacher/TeacherPage";
        }else {
            return "login";
        }
    }

    @RequestMapping("/register_user")
    public String register_user(User user,Model model,HttpSession session){
        if (userService.save(user).equals("注册成功")){
            return "login";
        }else {
            return "register";
        }
    }

}

